/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.pcm.service.impl;

import me.zhengjie.modules.cspimp.pcm.domain.FenceStrike;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.pcm.repository.FenceStrikeRepository;
import me.zhengjie.modules.cspimp.pcm.service.FenceStrikeService;
import me.zhengjie.modules.cspimp.pcm.service.dto.FenceStrikeDto;
import me.zhengjie.modules.cspimp.pcm.service.dto.FenceStrikeQueryCriteria;
import me.zhengjie.modules.cspimp.pcm.service.mapstruct.FenceStrikeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-06-27
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FenceStrikeServiceImpl implements FenceStrikeService {

    private final FenceStrikeRepository fenceStrikeRepository;
    private final FenceStrikeMapper fenceStrikeMapper;

    @Override
    public Map<String,Object> queryAll(FenceStrikeQueryCriteria criteria, Pageable pageable){
        Page<FenceStrike> page = fenceStrikeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fenceStrikeMapper::toDto));
    }

    @Override
    public List<FenceStrikeDto> queryAll(FenceStrikeQueryCriteria criteria){
        return fenceStrikeMapper.toDto(fenceStrikeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public FenceStrikeDto findById(Long id) {
        FenceStrike fenceStrike = fenceStrikeRepository.findById(id).orElseGet(FenceStrike::new);
        ValidationUtil.isNull(fenceStrike.getId(),"FenceStrike","id",id);
        return fenceStrikeMapper.toDto(fenceStrike);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FenceStrikeDto create(FenceStrike resources) {
        return fenceStrikeMapper.toDto(fenceStrikeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FenceStrike resources) {
        FenceStrike fenceStrike = fenceStrikeRepository.findById(resources.getId()).orElseGet(FenceStrike::new);
        ValidationUtil.isNull( fenceStrike.getId(),"FenceStrike","id",resources.getId());
        fenceStrike.copy(resources);
        fenceStrikeRepository.save(fenceStrike);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            fenceStrikeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FenceStrikeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FenceStrikeDto fenceStrike : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", fenceStrike.getCreateTime());
            map.put("更新时间", fenceStrike.getUpdateTime());
            map.put("创建者", fenceStrike.getCreateBy());
            map.put("更新者", fenceStrike.getUpdateBy());
            map.put(" name",  fenceStrike.getName());
            map.put("所属公司", fenceStrike.getCid());
            map.put("所属项目", fenceStrike.getPid());
            map.put("报警状态", fenceStrike.getAlarmStatusCode());
            map.put("报警状态名称", fenceStrike.getAlarmStatusName());
            map.put("类型code", fenceStrike.getAlarmTypeCode());
            map.put("类型名称", fenceStrike.getAlarmTypeName());
            map.put("报警时间", fenceStrike.getAlarmTime());
            map.put("用户/围栏信息", fenceStrike.getUserWlMess());
            map.put("标签设备", fenceStrike.getTagEquip());
            map.put("处理时间", fenceStrike.getDealTime());
            map.put("处理信息", fenceStrike.getDealMess());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}